package com.example.shop_2208.fragment

import android.content.Context
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.bumptech.glide.Glide
import com.example.shop_2208.R
import com.example.shop_2208.adapter.CategoryAdapter
import com.example.shop_2208.adapter.GoodsAdapter
import com.example.shop_2208.databinding.FragmentIndexBinding
import com.example.shop_2208.entity.CategoryEntity
import com.example.shop_2208.mvvm.BaseFragment
import com.example.shop_2208.net.Const
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.youth.banner.BannerConfig
import com.youth.banner.loader.ImageLoader

class HomeFragment : BaseFragment<FragmentIndexBinding,HomeViewModel>() {
    val imglist = mutableListOf<String>()//存放图片网址的集合

    lateinit var categoryAdapter: CategoryAdapter

    lateinit var goodsAdapter: GoodsAdapter
    var cateId = 14
    var page = 1
    override fun initData() {
        viewModel.bannerSuccess.observe(this){
            if(it.code==Const.CODE_SUCCESS){
                it.data.forEach {
                    imglist.add(it.imagePath)
                }
                binding.banner.setImages(imglist)
                    .setImageLoader(object :ImageLoader(){
                        override fun displayImage(
                            context: Context?,
                            path: Any?,
                            imageView: ImageView?
                        ) {
                            if (context != null&&imageView!=null) {
                                Glide.with(context).load(path).into(imageView)
                            }
                        }
                    }).setDelayTime(2000).setBannerStyle(BannerConfig.NUM_INDICATOR)
                    .start()
            }
        }
        //观察分类数据展示
        viewModel.cateSuccess.observe(this){
            if(it.code==Const.CODE_SUCCESS){
                val data = it.data[0].data
                if(data!=null){
                    categoryAdapter.data.addAll(data)
                }
                categoryAdapter.notifyDataSetChanged()

            }
        }
        //观察商品数据展示
        viewModel.goodsSuccess.observe(this){
            if(it.code==Const.CODE_SUCCESS){
                if(page==1){
                    //只有第一页时清空，不然加载更多时，只显示第二页的数据
                    goodsAdapter.data.clear()
                }
                goodsAdapter.data.addAll(it.data)
                goodsAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun initView() {
        //获取轮播图数据
        viewModel.banner()
        //初始化recyclerView
        categoryAdapter = CategoryAdapter()
        binding.rvCate.adapter = categoryAdapter
        binding.rvCate.layoutManager = GridLayoutManager(context,5)
        //获取分类数据
        viewModel.category()

        //初始化商品rv
        goodsAdapter = GoodsAdapter()
        binding.rvGoods.adapter = goodsAdapter
        //binding.rvGoods.layoutManager = LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false)
        binding.rvGoods.layoutManager = GridLayoutManager(context,2)
        //获取商品数据
        viewModel.goods(cateId,page,10)

        //刷新加载更多
        binding.srl.setOnRefreshLoadMoreListener(object :OnRefreshLoadMoreListener{
            override fun onRefresh(refreshLayout: RefreshLayout) {
                //刷新--从第一页开始展示
                page=1
                viewModel.goods(cateId,page,10)
                binding.srl.finishRefresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                //加载更多--在后续数据上进行拼接
                page++
                viewModel.goods(cateId,page,10)
                binding.srl.finishLoadMore()
            }
        })
        //点击分类，商品数据变化
        categoryAdapter.setOnItemClickListener { adapter, view, position ->
            cateId = categoryAdapter.data[position].id
            page = 1
            viewModel.goods(cateId,page,10)
        }
        binding.query.setOnClickListener {
            ARouter.getInstance().build("/app/search").navigation()
        }

        //点击item,跳转到详情界面
        goodsAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/detail").withInt("goodsId",goodsAdapter.data[position].id).navigation()
        }
    }

    override fun getViewModel(): Class<HomeViewModel>  = HomeViewModel::class.java

    override fun getLayout(): Int  = R.layout.fragment_index

}